package eerbaar.v2.app.view2;

import java.util.HashMap;
import java.util.Map;

import eerbaar.persona.IPersona;
import eerbaar.v2.app.core.ISprite;
import eerbaar.v2.app.images.Images;

public class ImageStore {

    private static final String BUG = "bug";
    private static final String PET = "pet";
    private static final String DOG = "dog";

    private final Map<String, ISprite> map = new HashMap<String, ISprite>();

    public ImageStore() {
        init();
    }

    public ISprite getImage(ImageId id) {
        return this.map.get(id.toString());
    }

    private void init() {

        Images images = Images.instance();
        loadImage("bone", null, images.bone());
        loadImage("bone.pack", null, images.bonePack());
        loadImage("house", null, images.house());
        loadImage("tree", null, images.tree());

        loadImage(PET, IPersona.State.REST, IPersona.Direction.LEFT,
                images.petsitleft());
        loadImage(PET, IPersona.State.REST, IPersona.Direction.RIGHT,
                images.petsit());
        loadImage(PET, IPersona.State.ACTIVE, IPersona.Direction.LEFT,
                images.dogleft());
        loadImage(PET, IPersona.State.ACTIVE, IPersona.Direction.RIGHT,
                images.dogright());

        loadImage(DOG, IPersona.State.REST, IPersona.Direction.LEFT,
                images.leadersitleft());
        loadImage(DOG, IPersona.State.REST, IPersona.Direction.RIGHT,
                images.leadersit());
        loadImage(DOG, IPersona.State.ACTIVE, IPersona.Direction.LEFT,
                images.leaderleft());
        loadImage(DOG, IPersona.State.ACTIVE, IPersona.Direction.RIGHT,
                images.leaderright());
        // loadImage(DOG, null, images.leadersit());

        loadImage(BUG, IPersona.State.REST, IPersona.Direction.LEFT,
                images.bug());
        loadImage(BUG, IPersona.State.REST, IPersona.Direction.RIGHT,
                images.bug());
        loadImage(BUG, IPersona.State.ACTIVE, IPersona.Direction.LEFT,
                images.bug());
        loadImage(BUG, IPersona.State.ACTIVE, IPersona.Direction.RIGHT,
                images.bug());
        loadImage(BUG, null, images.bug());
    }

    private void loadImage(String name, IPersona.State state,
            IPersona.Direction direction, ISprite resource) {

        final ImageId id = new ImageId(name, state, direction);
        ISprite image = this.map.get(id);

        if (image == null) {
            this.map.put(id.toString(), resource);
        }
    }

    private void loadImage(String name, IPersona.State state, ISprite resource) {

        loadImage(name, state, null, resource);
    }
}
